package tk.sunlatus.project.usermanage.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import tk.sunlatus.project.usermanage.domain.User;

/**
 * 用户管理持久化层，jdbc实现方式
 * 
 * @author sunlotus
 */
public class UserDaoJDBCImplTest
//implements UserDao {
{
    private Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://127.0.0.1:3306/sunlotus";
        String user = "sunlotus";
        String password = "killer";
        return DriverManager.getConnection(url, user, password);

    }

    public User queryByID(String userID) throws ClassNotFoundException {
        Connection conn = null;
        User user = new User();
        try {
            conn = getConnection();
            Statement stmt = conn.createStatement();
            String sql = "select * from user where id='" + userID + "'";
            ResultSet rs = stmt.executeQuery(sql);
            while(rs.next()){
                user.setUserID(rs.getString("id"));
                user.setName(rs.getString("name"));
                user.setPassword(rs.getString("password"));
            }
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw e;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return user;
    }
    
    
    public void testThrowException() throws ClassNotFoundException{
        throw new ClassNotFoundException();
    }
}
